package com.book.utils.JWT;

import cn.hutool.core.date.DateUtil;
import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;
import com.book.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.Date;

@Component
public class JwtTokenUtils {
    private static UserService staticUserService;
    private static final Logger log = LoggerFactory.getLogger(JwtTokenUtils.class);

    @Resource
    private UserService userService;

    @PostConstruct
    public void setUserService() {
        staticUserService = userService;
    }

    /**
     * 生成token信息
     * @param userId
     * @param password
     * @return 生成的token
     */
    public static String genToken(String userId, String password) {
        return JWT.create().withAudience(userId) //将userId 保存 到token 里面， 作为载荷
                .withExpiresAt(DateUtil.offsetHour(new Date(), 2))//设置两小时后超时  hutool  设置时间偏移两小时
                .sign(Algorithm.HMAC256(password));   //以password 作为token的密钥
    }



}
